{#**
 * 2007-2020 PrestaShop SA and Contributors
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * https://opensource.org/licenses/OSL-3.0
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to https://www.prestashop.com for more information.
 *
 * @author    PrestaShop SA <contact@prestashop.com>
 * @copyright 2007-2020 PrestaShop SA and Contributors
 * @license   https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
 * International Registered Trademark & Property of PrestaShop SA
 *#}

<table class="table mb-3">
  <thead>
  <tr>
    <th>
      {{ 'Date'|trans({}, 'Admin.Global') }}
    </th>
    <th>
      {{ 'Document'|trans({}, 'Admin.Orderscustomers.Feature') }}
    </th>
    <th>
      {{ 'Number'|trans({}, 'Admin.Orderscustomers.Feature') }}
    </th>
    <th>
      {{ 'Amount'|trans({}, 'Admin.Global') }}
    </th>
    <th class="text-right d-print-none">
      {{ 'Actions'|trans({}, 'Admin.Global') }}
    </th>
  </tr>
  </thead>
  <tbody>
  {% if  orderForViewing.documents.documents is not empty %}
    {% for document in orderForViewing.documents.documents %}
      <tr>
        <td>
          {{ document.createdAt|date_format_lite }}
        </td>
        <td>
          {% if document.type == 'invoice' %}
            {{ 'Invoice'|trans({}, 'Admin.Global') }}
          {% elseif document.type == 'credit_slip' %}
            {{ 'Credit slip'|trans({}, 'Admin.Orderscustomers.Feature') }}
          {% else %}
            {{ 'Delivery slip'|trans({}, 'Admin.Orderscustomers.Feature') }}
          {% endif %}
        </td>
        <td>
          {% if document.type == 'invoice' %}
            <a class="_blank"
               href="{{ getAdminLink('AdminPdf', true, {'submitAction': 'generateInvoicePDF', 'id_order_invoice': document.id}) }}"
            >
              {{ document.referenceNumber }}
            </a>
          {% elseif document.type == 'delivery_slip' %}
            <a class="_blank"
               href="{{ getAdminLink('AdminPdf', true, {'submitAction': 'generateDeliverySlipPDF', 'id_order_invoice': document.id}) }}"
            >
              {{ document.referenceNumber }}
            </a>
          {% elseif document.type == 'credit_slip' %}
            <a href="{{ getAdminLink('AdminPdf', true, {'submitAction': 'generateOrderSlipPDF', 'id_order_slip': document.id}) }}">
              {{ document.referenceNumber }}
            </a>
          {% endif %}
        </td>
        <td>
          {% if document.amount %}
            <p class="mb-0">
              {{ document.amount }}

              {% if document.amountMismatch %}
                ({{ document.amountMismatch }})
              {% endif %}
            </p>
          {% else %}
            --
          {% endif %}
        </td>
        <td class="text-right">
          {% if document.type == 'invoice' %}
            {% if (document.isAddPaymentAllowed and document.amount) %}
              <button href="#" class="js-enter-payment-btn btn btn-primary btn-sm" data-payment-amount="{{ document.numericalAmount }}">
                {{ 'Enter payment'|trans({}, 'Admin.Orderscustomers.Feature') }}
              </button>
            {% endif %}
            <button href="#" class="js-open-invoice-note-btn btn btn-primary btn-sm">
              {% if document.note is empty %}
                {{ 'Add note'|trans({}, 'Admin.Orderscustomers.Feature') }}
              {% else %}
                {{ 'Edit note'|trans({}, 'Admin.Orderscustomers.Feature') }}
              {% endif %}
            </button>
          {% endif %}
        </td>
      </tr>
      {% if document.type == 'invoice' %}
        <tr class="d-none">
          <td colspan="5">
            <form action="{{ path('admin_orders_update_invoice_note', {
              'orderId': orderForViewing.id,
              'orderInvoiceId': document.id
            }) }}" method="post">
              <div class="input-group">
                <input type="text" class="form-control" name="invoice_note" value="{{ document.note }}">
                <button class="btn btn-secondary ml-2 btn-sm js-cancel-invoice-note-btn" type="button">
                  {{ 'Cancel'|trans({}, 'Admin.Actions') }}
                </button>
                <button class="btn btn-primary ml-2 btn-sm" type="submit">
                  {{ 'Save'|trans({}, 'Admin.Actions') }}
                </button>
              </div>
            </form>
          </td>
        </tr>
      {% endif %}
    {% endfor %}
  {% else %}
    <tr>
      <td colspan="5" class="text-center">
        {{ 'There is no available document'|trans({}, 'Admin.Orderscustomers.Notification') }}
      </td>
    </tr>
  {% endif %}
  </tbody>
</table>

{% if  orderForViewing.documents.documents is empty and orderForViewing.invoiceManagementIsEnabled %}
  <form action="{{ path('admin_orders_generate_invoice', {'orderId': orderForViewing.id}) }}" method="POST">
    <button class="btn btn-primary">
      <i class="material-icons">autorenew</i>
      {{ 'Generate invoice'|trans({}, 'Admin.Orderscustomers.Feature') }}
    </button>
  </form>
{% endif %}
